package at.main.gui;

import at.main.android.Graphics;
import at.main.android.Sprite;
import at.main.util.AllImage;
import at.main.util.ConfigPanel;
import at.main.util.ConstGame;

public class PacMan {
	public static int iStatus = 1;

	public static final int Left = 1;

	public static final int Right = 0;

	public static final int Up = 3;

	public static final int Down = 2;

	public static int iX = 0;

	public static int iY = 0;

	public static int iV = 0;

	public static int iCountlife = 0; // So man song

	public static long iScore = 0; // So diem

	public static int iLevel = 0;

	public static int iCountPickup = 0; // So iTems nhặt được

	public static int iCountItemsEta = 0;

	public static int iCountMonsterEta = 0;

	public static boolean isDie = false;

	public static int iKeypress = iStatus;

	public int iIndex = 0;

	public int iStartCol = 0;

	public int iEndCol = 0;

	public int iStartRow = 0;

	public int iEndRow = 0;

	public static final int iSizeTile = 24;

	public int iNumColofCanvas = 0;

	public int iNumRowofCanvas = 0;

	public int iWidthMap = 0;

	public int iHeightMap = 0;

	public int iCurrCol = 0;

	public int iCurrRow = 0;

	public int iSaveX = 0;

	public int iSaveY;

	public boolean isCheckGo = true;

	public long lTimer = 0;

	// Khoi tao cac gia tri ban dau cho PacMan
	public PacMan() {
		initPacman();
		iV = 4;
		iScore = 0;
		iLevel = 1;
		iCountlife = 3;
		iCountPickup = 0;
		iCountItemsEta = 0;
		iCountMonsterEta = 0;
	}

	public PacMan(int v, long sroce, int level) {
		initPacman();
		iV = v;
		iScore = sroce;
		iLevel = level;
		iCountlife = 3;
		iCountPickup = 0;
		iCountItemsEta = 0;
		iCountMonsterEta = 0;
	}

	// Khoi toa vi tri ban dau cho con pacman
	public void initPacman() {		
		isDie = false;
		iIndex = 0;
		iStatus = Left;
		iKeypress = 4;//Left
		System.out.println(">>>>>>>>> iStatus " + iStatus);
		iX = ConstGame.iXPosPacMan;
		iY = ConstGame.iYPosPacMan + ConfigPanel.heightPanel();
		iSaveX = ConstGame.iXPosPacMan;
		iSaveY = ConstGame.iYPosPacMan + ConfigPanel.heightPanel();
		iWidthMap = PlayGame.k_row * iSizeTile;
		iHeightMap = PlayGame.k_column * iSizeTile;
		MyCanvas.iTranslateX = iX - MyCanvas.iWidth / 2;
		MyCanvas.iTranslateY = iY - MyCanvas.iHeight / 2;
		iNumColofCanvas = MyCanvas.iWidth / iSizeTile;
		// System.out.println(">>>>>>>>>>>>>>>>>>>>>>>IX" + iX + " " + iY);
		iStartCol = (iX - MyCanvas.iWidth / 2) / iSizeTile - 1;
		iEndCol = iStartCol + iNumColofCanvas + 2;
		iNumRowofCanvas = MyCanvas.iHeight / iSizeTile;
		iStartRow = (iY - MyCanvas.iHeight / 2) / iSizeTile - 1;
		iEndRow = iStartRow + iNumRowofCanvas + 2;
	}

	// Ve pacman
	public void paint(Graphics g) {
		System.out.println(">>>>>>>>> iStatus " + iStatus);
		g.drawRegion(AllImage.img_arrPacMan[iStatus], 0, iIndex * 24, 24, 24,
				Sprite.TRANS_NONE, iX, iY, Graphics.LEFT | Graphics.TOP);

	}

	// Kiem tra va cham cua pacman voi monster
	// x1 , y1 : Toa do con pacman
	// x2 , y 2 : Toa do con monster
	public boolean collision(int x1, int y1, int x2, int y2) {
		if (x2 <= x1 + iSizeTile && x1 <= x2 + iSizeTile && y2 <= y1
				&& y1 <= y2 + iSizeTile)
			return true;
		return false;
	}

	// Cap nhat lai map thi PacMan an
	public void CheckPacManEta(int i, int j) {
		// An duoc man
		// AllSound.aCoin.stopSound();
		// AllSound.aItems.stopSound();

		if (MyCanvas.instanMyCanvas.playGame.arrMap[j][i] == ConstGame.iTileEta) {
			iScore++;
			iCountPickup++;
			MyCanvas.instanMyCanvas.playGame.arrMap[j][i] = ConstGame.iTileNone;
			// if(SoundPage.isSound){
			// if(AllSound.aCoin.player.getState() == Player.STARTED)
			// return ;
			// AllSound.aCoin.playSound();
			// System.out.println(">>>aCoin");
			// }
		}
		// An duoc iTems
		if (MyCanvas.instanMyCanvas.playGame.arrMap[j][i] == ConstGame.iTileItems) {
			MyCanvas.instanMyCanvas.playGame.arrMap[j][i] = ConstGame.iTileNone;
			iScore += ConstGame.iSroceEta;
			// if(SoundPage.isSound)
			// AllSound.aItems.playSound();
			//			
			ManagerMonster.isState = false;
			lTimer = System.currentTimeMillis();
		}
	}

	public boolean CheckPacManToMonster() {
		// Check vo cham voi con monster
		for (int l = 0; l < ManagerMonster.iCountMonster; l++) {
			if (ManagerMonster.arrMonster[l].isLife) {
				// Trang thai no true
				// Neu pacMan va cham vao thi se bi chet
				if (collision(iX, iY, ManagerMonster.arrMonster[l].iX,
						ManagerMonster.arrMonster[l].iY)) {
					if (ManagerMonster.isState) {
						// Them mot dang xu ly de nhin thay hien thuc hon
						iCountlife--;
						if (iCountlife == 0) {
							PlayGame.isGameOver = true;
							MyCanvas.clearInput();
						} else {
							PacMan.isDie = true;
						}
						return true;
					} else {
						ManagerMonster.arrMonster[l].isLife = false;
						iScore += 200;
						iCountMonsterEta++;
					}
				}
			}
		}
		return false;
	}

	// Check va cham cua pacman
	public boolean CheckPacManGo(int Row, int Col) {
		Col = Col - ConfigPanel.heightPanel();
		Row = Row / iSizeTile;
		Col = Col / iSizeTile;
		// Xet cho con pacman di xuyen tuong
		// Top
		if (Col < 0) {
			iY = (PlayGame.k_column - 1) * iSizeTile
					+ ConfigPanel.heightPanel();// 726
			MyCanvas.iTranslateY = iY - MyCanvas.iHeight + iSizeTile;
			iStartRow = PlayGame.k_column - iNumRowofCanvas - 2;
			iEndRow = PlayGame.k_column;
			return false;
		}
		// Botton
		if (Col > 29) {
			iY = ConfigPanel.heightPanel();
			MyCanvas.iTranslateY = 0;
			iStartRow = 0;
			iEndRow = iNumRowofCanvas + 1;
			return false;
		}
		// Left
		if (Row < 0) {
			iX = (PlayGame.k_row - 1) * iSizeTile;
			MyCanvas.iTranslateX = iX - MyCanvas.iWidth + iSizeTile;
			iStartCol = PlayGame.k_row - iNumColofCanvas - 2;
			iEndCol = PlayGame.k_row;
			return false;
		}
		// Right
		if (Row > 29) {
			iX = 0;
			MyCanvas.iTranslateX = 0;
			iStartCol = 0;
			iEndCol = iNumColofCanvas;
			return false;
		}

		// Xet va cham voi tuong
		switch (MyCanvas.instanMyCanvas.playGame.arrMap[Col][Row]) {
		case ConstGame.iTileWall:
			return false;
		}
		return true;
	}

	// Check da qua level chưa
	public boolean CheckContineLevel() {
		if (iCountPickup == PlayGame.iCountItems) {
			PlayGame.isContineLevle = true;
			return true;
		}
		return false;
	}

	// Tao hang so
	public final int k_anpa = 12;

	public final int k_anpa1 = 12;

	public int iDislayDie = 0;

	public int iIndexTilePacManEta = ConstGame.iIndexTilePacManEta + 1;

	public int iTemp = 0;

	public void CheckPacManDie() {
		if (isDie) {
			iTemp++;
			if (iTemp > (15 + iV)) {
				iTemp = 0;
				if (iIndexTilePacManEta < ConstGame.iCountTilePacMan - 1) {
					iIndexTilePacManEta++;
					iIndex = iIndexTilePacManEta;
				} else {
					iIndexTilePacManEta = ConstGame.iIndexTilePacManEta + 1;
					iIndex = 0;
					isDie = false;
					PlayGame.isReady = false;
					// Khoi tao lai con Pacman
					initPacman();
					// Khoi tao lai cac con monster
					ManagerMonster.initManagerMonster();
				}
			}
		}
	}

	// Cap nhat lai trang thai, vi tri cua pacMan
	public void update() {

		// Check noi co van cham voi cac con monster
		if (!isDie) {
			CheckPacManToMonster();
			iTemp = 0;
		} else {
			MyCanvas.clearInput();
			CheckPacManDie();
		}

		// Check qua level moi chưa
		CheckContineLevel();
		// Right
		//System.out.println("PacMan X: " + iX + " Y : " + iY);
		System.out.println(">>>iKeypress>>>>. "+ iKeypress);
		int i = 0;
		int j = 0;
		// Right
		if (MyCanvas.keyHold[6]) {
			MyCanvas.clearInput1(6);
			// Row = Row / iSizeTile;
			// Col = Col / iSizeTile;
			if ((iY - ConfigPanel.heightPanel()) % iSizeTile == 0
					&& CheckPacManGo(iX + iSizeTile, iY))
				iKeypress = 6;
		}
		// Left
		if (MyCanvas.keyHold[4]) {
			MyCanvas.clearInput1(4);
			if (((iY - ConfigPanel.heightPanel()) % iSizeTile == 0) && CheckPacManGo(iX-iSizeTile, iY))
				iKeypress = 4;
		}
		// Up
		if (MyCanvas.keyHold[2]) {
			MyCanvas.clearInput1(2);
			if ((iX % iSizeTile == 0) && CheckPacManGo(iX, iY-iSizeTile)) {
				iKeypress = 2;
			}
		}
		// Down
		if (MyCanvas.keyHold[8]) {
			MyCanvas.clearInput1(8);
			if ((iX % iSizeTile == 0) && CheckPacManGo(iX, iY + iSizeTile)) {
				iKeypress = 8;
			}
		}
		
		
		// Right
		if (iKeypress == 6) {
			iStatus = Right;
			iIndex++;
			if (iIndex > ConstGame.iIndexTilePacManEta - 1) {
				iIndex = 0;
			}
			// Check va cham
			if (CheckPacManGo(iX + iSizeTile, iY)) {
				iX += iV;
				if (MyCanvas.iTranslateX + iX > MyCanvas.iWidth / 2) {
					if ((MyCanvas.iTranslateX + MyCanvas.iWidth) < iWidthMap) {
						MyCanvas.iTranslateX += iV;
						iStartCol = Math.max(0, MyCanvas.iTranslateX
								/ iSizeTile);
						iEndCol = iStartCol + iNumColofCanvas + 2;
						if (iEndCol > MyCanvas.instanMyCanvas.playGame.arrMap.length) {
							iEndCol = 30;
						}
					}
				}
				// Cap nhat lai
				i = iX / iSizeTile;
				j = (iY - ConfigPanel.heightPanel()) / iSizeTile;
				CheckPacManEta(i, j);
			} else {
				iIndex = 0;
			}
		}

		// Left
		if (iKeypress == 4) {
			iStatus = Left;
			iIndex++;
			if (iIndex > ConstGame.iIndexTilePacManEta - 1) {
				iIndex = 0;
			}
			// Check va cham
			if (CheckPacManGo(iX - iV, iY)) {
				iX -= iV;
				if (iX - MyCanvas.iTranslateX < MyCanvas.iWidth / 2) {
					if (MyCanvas.iTranslateX > 0) {
						MyCanvas.iTranslateX -= iV;
						iStartCol = Math.max(0, MyCanvas.iTranslateX
								/ iSizeTile);
						iEndCol = iStartCol + iNumColofCanvas + 2;
						if (iEndCol > MyCanvas.instanMyCanvas.playGame.arrMap.length) {
							iEndCol = 30;
						}
					}
				}
				i = (iX + 16) / iSizeTile;
				j = (iY - ConfigPanel.heightPanel()) / iSizeTile;
				CheckPacManEta(i, j);
			} else {
				iIndex = 0;
			}
		}

		// Up
		if (iKeypress == 2) {
			iStatus = Up;
			iIndex++;
			if (iIndex > ConstGame.iIndexTilePacManEta - 1) {
				iIndex = 0;
			}
			// Check va cham
			if (CheckPacManGo(iX, iY - iV)) {
				iY -= iV;
				if (iY - MyCanvas.iTranslateY < MyCanvas.iHeight / 2) {
					if (MyCanvas.iTranslateY > iV) {
						MyCanvas.iTranslateY -= iV;
						iStartRow = Math.max(0, MyCanvas.iTranslateY
								/ iSizeTile);
						iEndRow = iStartRow + iNumRowofCanvas + 1;
						if (iEndRow > MyCanvas.instanMyCanvas.playGame.arrMap.length) {
							iEndRow -= 1;
						}
					} else {
						MyCanvas.iTranslateY = 0;
					}
				}
				i = iX / iSizeTile;
				j = ((iY - ConfigPanel.heightPanel()) + 16) / iSizeTile;
				CheckPacManEta(i, j);

			} else {
				iIndex = 0;
			}
		}
		// Down
		if (iKeypress == 8) {
			iStatus = Down;
			iIndex++;
			if (iIndex > ConstGame.iIndexTilePacManEta - 1) {
				iIndex = 0;
			}
			if (CheckPacManGo(iX, iY + iSizeTile)) {
				iY += iV;
				if (MyCanvas.iTranslateY + iY > MyCanvas.iHeight / 2) {
					if (MyCanvas.iTranslateY - ConfigPanel.heightPanel()
							+ MyCanvas.iHeight < iHeightMap - iV) {
						MyCanvas.iTranslateY += iV;
						iStartRow = Math.max(0, MyCanvas.iTranslateY
								/ iSizeTile);
						iEndRow = iStartRow + iNumRowofCanvas + 1; // Tai sao
						if (iEndRow > MyCanvas.instanMyCanvas.playGame.arrMap.length) {
							iEndRow -= 1;
						}
					}
				}
				i = iX / iSizeTile;
				j = (iY - ConfigPanel.heightPanel()) / iSizeTile;
				CheckPacManEta(i, j);

			} else {
				iIndex = 0;
			}
		}
		// Cap nhat lai trang thai cho cac con monster
		if (System.currentTimeMillis() - lTimer > ConstGame.lDislayMonster) {
			ManagerMonster.isState = true;
		}
	}
}
